package com.merelyb.util;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by lim on 16/8/31.
 * Excel读取操作
 */
public class JxlReadExcelUtils {

    /**
     * 获取工作簿
     * @param inputStream
     * @return
     */
    public Workbook ReadExcel(InputStream inputStream){
        Workbook workbook = null;
        try {
            workbook =  Workbook.getWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return workbook;
    }

    /**
     * 获取工作簿
     * @param file
     * @return
     */
    public Workbook ReadExcel(File file){
        Workbook workbook = null;
        try{
            workbook = Workbook.getWorkbook(file);
        }catch (IOException e){
            e.printStackTrace();
        }catch (BiffException e){
            e.printStackTrace();
        }

        return workbook;
    }

    /**
     * 获取所有sheet页面名称
     * @param workbook
     * @return
     */
    public String[] getSheetNames(Workbook workbook){
        return workbook.getSheetNames();
    }

    /**
     * 获取所有sheet
     * @param workbook
     * @return
     */
    public Sheet[] getSheets(Workbook workbook){
        return workbook.getSheets();
    }

    /**
     * 根据索引获取sheet
     * @param workbook
     * @param iIndex
     * @return
     */
    public Sheet getSheet(Workbook workbook, int iIndex){
        Sheet sheet = null;
        try{
            sheet = workbook.getSheet(iIndex);
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
        }
        return sheet;
    }

    /**
     * 根据名称获取sheet
     * @param workbook
     * @param sheetName
     * @return
     */
    public Sheet getSheet(Workbook workbook, String sheetName){
        return workbook.getSheet(sheetName);
    }

    /**
     * 获取表格
     * @param sheet
     * @param iRow
     * @param iCol
     * @return
     */
    public Cell getCell(Sheet sheet, int iRow, int iCol){
        return sheet.getCell(iRow, iCol);
    }
}
